package com.dailyblue.java.spring.rocketmq.普通消息.消息生产;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

public class 同步消息的生产 {

    public static void main(String[] args) throws Exception {
        // 创建消息生产者 producer，并指定生产者组名
        DefaultMQProducer producer = new DefaultMQProducer("zhuoyue01");
        // 指定 NameSrv 地址
        producer.setNamesrvAddr("192.168.160.88:9876");
        // 启动 producer
        producer.start();
        // 创建消息对象，指定 Topic、Tag 和消息体
        for (int i = 1; i < 11; i++) {
            String topic = "TopicA";
            String tag = "TagA";
            String body = "Hello,Rocket,index:" + i;
            Message messsage = new Message(topic, tag, body.getBytes(StandardCharsets.UTF_8));
            // 发送消息
            SendResult result = producer.send(messsage);
            System.out.println(result);
            Thread.sleep(1000);
        }
        // 关闭生产者 producer
        producer.shutdown();
    }
}
